{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dfc38f06-61ab-410c-82ec-a4b5f6079986",
   "metadata": {
    "execution": {
     "shell.execute_reply.end": "2023-03-10T03:53:27.077296Z",
     "shell.execute_reply.started": "2023-03-10T03:53:26.981842Z",
     "to_execute": "2023-03-10T03:53:26.892Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from trailblazer4ml.causal_discovery.datasets.easydataset import EasyDataset\n",
    "from trailblazer4ml.causal_discovery.estimators.GNN_based import GNN_struct\n",
    "from trailblazer4ml.causal_discovery.estimators.config import CONFIG\n",
    "import warnings\n",
    "warnings.simplefilter(\"ignore\")\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# from causal_discovery.estimators.GNN_based import MLPEncoder, MLPDEncoder, SEMEncoder\n",
    "# from causal_discovery.estimators.GNN_based import MLPDDecoder, MLPDiscreteDecoder, MLPDecoder, SEMDecoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4f1b0850-e822-49d1-abd8-1b6147d37da4",
   "metadata": {
    "execution": {
     "shell.execute_reply.end": "2023-03-10T03:53:29.984450Z",
     "shell.execute_reply.started": "2023-03-10T03:53:29.877556Z",
     "to_execute": "2023-03-10T03:53:29.731Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "class CONFIG:\n",
    "    \"\"\"Dataclass with app parameters\"\"\"\n",
    "\n",
    "    def __init__(self):\n",
    "        pass\n",
    "\n",
    "    # Epochs\n",
    "    #     epochs = 300\n",
    "    epochs = 5\n",
    "\n",
    "    # Batch size (note: should be divisible by sample size, otherwise throw an error)\n",
    "    batch_size = 50\n",
    "\n",
    "    #     k_max_iter = 1e2\n",
    "    k_max_iter = 3\n",
    "\n",
    "    # Learning rate (baseline rate = 1e-3)\n",
    "    lr = 1e-3\n",
    "\n",
    "    x_dims = 1\n",
    "    z_dims = 1\n",
    "    optimizer = \"Adam\"\n",
    "    graph_threshold = 0.3\n",
    "    tau_A = 0.0\n",
    "    lambda_A = 0.0\n",
    "    c_A = 1\n",
    "    use_A_connect_loss = 0\n",
    "    use_A_positiver_loss = 0\n",
    "    no_cuda = True\n",
    "    seed = 42\n",
    "    encoder_hidden = 64\n",
    "    decoder_hidden = 64\n",
    "    temp = 0.5\n",
    "    encoder = \"mlp\"\n",
    "    decoder = \"mlp\"\n",
    "    no_factor = False\n",
    "    encoder_dropout = 0.0\n",
    "    decoder_dropout = (0.0,)\n",
    "    h_tol = 1e-8\n",
    "    lr_decay = 200\n",
    "    gamma = 1.0\n",
    "    prior = False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d73de4ba-da85-4df7-b51d-258f9ed37286",
   "metadata": {
    "execution": {
     "shell.execute_reply.end": "2023-03-10T03:53:30.222870Z",
     "shell.execute_reply.started": "2023-03-10T03:53:30.190055Z",
     "to_execute": "2023-03-10T03:53:30.058Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>treat</th>\n",
       "      <th>age</th>\n",
       "      <th>educ</th>\n",
       "      <th>black</th>\n",
       "      <th>hispan</th>\n",
       "      <th>married</th>\n",
       "      <th>nodegree</th>\n",
       "      <th>re74</th>\n",
       "      <th>re75</th>\n",
       "      <th>re78</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>37</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>9930.0460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>22</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3595.8940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24909.4500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>27</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7506.1460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>33</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>289.7899</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   treat  age  educ  black  hispan  married  nodegree  re74  re75        re78\n",
       "0      1   37    11      1       0        1         1   0.0   0.0   9930.0460\n",
       "1      1   22     9      0       1        0         1   0.0   0.0   3595.8940\n",
       "2      1   30    12      1       0        0         0   0.0   0.0  24909.4500\n",
       "3      1   27    11      1       0        0         1   0.0   0.0   7506.1460\n",
       "4      1   33     8      1       0        0         1   0.0   0.0    289.7899"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"../trailblazer/trailblazer4ml/causal_inference/datasets/data/lalonde.csv\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ce85563e-a437-4637-97a5-d60763c42c4c",
   "metadata": {
    "execution": {
     "shell.execute_reply.end": "2023-03-10T03:53:34.074852Z",
     "shell.execute_reply.started": "2023-03-10T03:53:33.980707Z",
     "to_execute": "2023-03-10T03:53:33.867Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "df = (df - df.min())/(df.max() - df.min())\n",
    "covariates = ['age', 'educ', 'black', 'hispan', 'married', 'nodegree']\n",
    "treat = ['treat']\n",
    "outcomes = ['re78']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e6ac35d4-7ea6-454b-b7a6-35224ef3d4a8",
   "metadata": {
    "execution": {
     "shell.execute_reply.end": "2023-03-10T03:53:34.587275Z",
     "shell.execute_reply.started": "2023-03-10T03:53:34.575864Z",
     "to_execute": "2023-03-10T03:53:34.433Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "data = EasyDataset(df, covariates, treat, outcomes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0cda0003-7dc3-4618-a929-a5aefeef3186",
   "metadata": {
    "execution": {
     "shell.execute_reply.end": "2023-03-10T03:53:37.184708Z",
     "shell.execute_reply.started": "2023-03-10T03:53:37.175470Z",
     "to_execute": "2023-03-10T03:53:37.030Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data size: 614\n"
     ]
    }
   ],
   "source": [
    "G_struct = GNN_struct(data, CONFIG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "64db2b2d-f891-4369-ac7a-53d494ea5393",
   "metadata": {
    "execution": {
     "shell.execute_reply.end": "2023-03-10T03:53:39.479664Z",
     "shell.execute_reply.started": "2023-03-10T03:53:39.394164Z",
     "to_execute": "2023-03-10T03:53:39.248Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['age', 'educ', 'black', 'hispan', 'married', 'nodegree', 'treat',\n",
       "       're78'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "G_struct._get_feature_columns()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7b1ee5ea-fc47-4a26-bdac-5c5cdd8d569e",
   "metadata": {
    "execution": {
     "shell.execute_reply.end": "2023-03-10T03:56:24.288652Z",
     "shell.execute_reply.started": "2023-03-10T03:53:40.286882Z",
     "to_execute": "2023-03-10T03:53:39.999Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nll_train: 0.0064705427 ELBO_loss: 0.0374975530 mse_train: 0.0016176357\n",
      "nll_train: 0.0044352690 ELBO_loss: 0.0198582211 mse_train: 0.0011088172\n",
      "nll_train: 0.0002812760 ELBO_loss: 0.0140660238 mse_train: 0.0000703190\n",
      "nll_train: 0.0002611126 ELBO_loss: 0.0139211914 mse_train: 0.0000652781\n",
      "nll_train: 0.0002507746 ELBO_loss: 0.0135293175 mse_train: 0.0000626937\n",
      "nll_train: 0.0002378926 ELBO_loss: 0.0134091243 mse_train: 0.0000594731\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>method</th>\n",
       "      <th>edge_start</th>\n",
       "      <th>edge_end</th>\n",
       "      <th>estimate_value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>mlp-mlp</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>mlp-mlp</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>mlp-mlp</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    method  edge_start  edge_end  estimate_value\n",
       "0  mlp-mlp           0         4               1\n",
       "1  mlp-mlp           1         5               1\n",
       "2  mlp-mlp           7         3               1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "G_struct._get_adjacent_matrix()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "record_timing": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
